package com.graduation.rescuematerial.controller;

import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.RespBean;
import com.graduation.rescuematerial.vo.RespBeanEnum;
import com.graduation.rescuematerial.vo.SendEmailVo;
import com.graduation.rescuematerial.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/13 21:43
 * @Version:
 * @Description:TODO
 */
@RestController
@Slf4j
@RequestMapping("register")
@Api(tags = "注册模块")
public class RegisterController {

    @Autowired
    private UserService userService;

//    @ApiOperation("本系统不需要普通注册：用户普通注册(不属于任何部门，权限默认是common_member)")
//    @PostMapping("/user")
//    @ResponseBody
//    @Transactional(rollbackFor = Exception.class)
//    public RespBean registerUser(@Valid @RequestBody RegisterVo registerVo){
//        return userService.register(registerVo);
//    }

    @ApiOperation("发送验证码")
    @PostMapping("/sendEmail")
    @ResponseBody
    public RespBean sendEmail(@RequestBody SendEmailVo sendEmailVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=sendEmailVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                return userService.sendEmail(sendEmailVo.getEmail(),sendEmailVo.getMessage());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("测试(插入数据的测试)")
    @PostMapping("/test")
    @ResponseBody
    public RespBean test(){
        return userService.test();
    }


    //注册模块还差：
    //1、系统管理员分配超级管理员和系统管理员
    //2、部门管理员添加部门管理员和部门成员
    //3、其余系统需要增加普通用户

//    @ApiOperation("系统管理员分配系统管理员")
//    @ResponseBody
//    @PostMapping("/sys/admin")
//    public RespBean registerSysAdmin(@RequestBody UserBaseVo userBaseVo, HttpServletRequest request, HttpServletResponse response){
//       //判断是否登录
//        LoginCheck loginCheck=new LoginCheck();
//        RespBean respBean=loginCheck.loginCheck(request,response);
//       if (respBean.getStatus()!=200){
//            return respBean;
//       }
//       UserVo userVo=(UserVo) respBean.getData();
//       //判断是否有操作权限
//       if (!userVo.getAction().equals("system_administrator")){
//           return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"没有操作权限");
//       }
//
//       return userService.registerSysAdmin(userBaseVo);
//    }



}
